package com.nacrtstudio.webgiftshop.pages;

import java.util.Set;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;

import com.nacrtstudio.webgiftshop.facade.Controller;
import com.nacrtstudio.webgiftshop.model.shop.PackedGift;

public class DeliveryDetails {
	@Inject
	private Request request;
	public void onActivate() {
		try {
			if (controller.getRequest()==null) {
	    		controller.setRequest(request);
			}
			for (PackedGift iterable : getPackedGifts()) {
				System.out.println("\n"+iterable.getChosenGiftId()+".)"+iterable.getGift().getGiftName());
			}
		} catch (Exception e) {
		}
	}
	@Property
	private PackedGift packGift;
	@Persist
	private Set<PackedGift> packedGifts;

	public Set<PackedGift> getPackedGifts() {
		return packedGifts;
	}

	public void setPackedGifts(Set<PackedGift> packedGifts) {
		this.packedGifts = packedGifts;
	}
	
	@InjectPage
	private GiftInfo giftInfo;
	Object onActionFromShow(Long id){
		giftInfo.setGift(controller.findGift(id));
    	return giftInfo;
    }

	@Inject
	@Property
	private Controller controller;
	@Component(id = "deliveryForm")
	private Form deliveryForm;
	
	@InjectPage
	private OrderPage orderPage;
	
	public Object onSuccessFromDeliveryForm(){
		for (PackedGift gift : getPackedGifts()) {
			gift.setShippingPrice(gift.getAddress().getTown().getShippingPrice());
		}
		orderPage.setPackedGifts(getPackedGifts());
		return orderPage;
	}
}
